package com.google.code.swingonfire.component.page;

import java.awt.Component;

/**
 * <p>
 * A container, that holds a component. The component can be switched. The transition is animated.
 * </p>
 * 
 * @author thred
 */
public class JPageComponent extends AbstractJPageComponent implements JPageComponentSelectionListener
{

    private static final long serialVersionUID = 3351952077808146552L;

    public JPageComponent()
    {
        super();

        addSelectionListener(this);
    }

    /**
     * Unsupported operation. Use setComponent(). {@inheritDoc}
     */
    @Override
    public Component add(Component component)
    {
        throw new UnsupportedOperationException("Add not supported");
    }

    /**
     * Unsupported operation. Use setComponent(). {@inheritDoc}
     */
    @Override
    public Component add(String name, Component component)
    {
        throw new UnsupportedOperationException("Add not supported");
    }

    /**
     * Unsupported operation. Use setComponent(). {@inheritDoc}
     */
    @Override
    public Component add(Component component, int index)
    {
        throw new UnsupportedOperationException("Add not supported");
    }

    /**
     * Unsupported operation. Use setComponent(). {@inheritDoc}
     */
    @Override
    public void add(Component component, Object constraints)
    {
        throw new UnsupportedOperationException("Add not supported");
    }

    /**
     * Unsupported operation. Use setComponent(). {@inheritDoc}
     */
    @Override
    public void add(Component component, Object constraints, int index)
    {
        throw new UnsupportedOperationException("Add not supported");
    }

    /**
     * Unsupported operation. Use setComponent(). {@inheritDoc}
     */
    @Override
    public void remove(Component component)
    {
        throw new UnsupportedOperationException("Remove not supported");
    }

    /**
     * Unsupported operation. Use setComponent(). {@inheritDoc}
     */
    @Override
    public void removeAll()
    {
        throw new UnsupportedOperationException("Remove not supported");
    }

    public void setNextComponent(Component component)
    {
        setComponent(component, true, isAnimationEnabled());
    }

    public void setPreviousComponent(Component component)
    {
        setComponent(component, false, isAnimationEnabled());
    }

    /**
     * Sets the component
     * 
     * @param component the component
     * @param forward true if forward, false if backward
     * @param animationEnabled true if animation enabled
     */
    public void setComponent(Component component, boolean forward, boolean animationEnabled)
    {
        if (forward)
        {
            int index = getMaximumComponentIndex() + 1;

            super.add(component, index);

            setMaximumComponentIndex(index);
            setSelectedComponentIndex(index, animationEnabled);
        }
        else
        {
            int index = getMinimumComponentIndex() - 1;

            super.add(component, index);

            setMinimumComponentIndex(index);
            setSelectedComponentIndex(index, animationEnabled);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public void componentSelected(JPageComponentSelectionEvent event)
    {
        if (!event.isAdjusting())
        {
            cleanupEntries();
        }
    }

}
